package cn.smbms.service.storagerecord;

import java.util.List;

import cn.smbms.mapper.storagerecord.StorageRecordMapper;
import cn.smbms.pojo.StorageRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(rollbackFor = {Exception.class})
@Service("storageRecordService")
public class StorageRecordServiceImpl implements StorageRecordService {
	@Autowired
	private StorageRecordMapper storageRecordMapper;


	@Override
	public boolean add(StorageRecord bill) {
		boolean flag = false;
		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			int add = sqlSession.getMapper(StorageRecordMapper.class).add(bill);
			if(add>0){
				sqlSession.commit();
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/

		if(storageRecordMapper.add(bill)>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public List<StorageRecord> getStorageRecordList(StorageRecord bill,int currentPageNo,int pageSize) {
		System.out.println("query productName ---- > " + bill.getProductName());
		System.out.println("query providerId ---- > " + bill.getProviderId());
		System.out.println("query isPayment ---- > " + bill.getIsPayment());
		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			int page = (currentPageNo-1)*pageSize;
			billList = sqlSession.getMapper(StorageRecordMapper.class).getStorageRecordList(bill,page,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		List<StorageRecord> billList = storageRecordMapper.getStorageRecordList(bill,currentPageNo,pageSize);
		return billList;
	}

	@Override
	public boolean deleteBillById(String delId) {
		boolean flag = false;
		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			int result = sqlSession.getMapper(StorageRecordMapper.class).deleteBillById(delId);
			if(result> 0) {
				flag = true;
				sqlSession.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		if(storageRecordMapper.deleteBillById(delId)>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public StorageRecord getBillById(String id) {

		/*try{
			sqlSession = MyBatisUtil.createSqlSession();
			StorageRecordMapper storageRecordMapper = sqlSession.getMapper(StorageRecordMapper.class);
			bill = storageRecordMapper.getBillById(id);
		}catch (Exception e) {
			e.printStackTrace();
			bill = null;
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		StorageRecord bill = storageRecordMapper.getBillById(id);
		return bill;
	}

	@Override
	public boolean modify(StorageRecord bill) {
		boolean flag = false;
		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			int result = sqlSession.getMapper(StorageRecordMapper.class).modify(bill);
			if(result > 0) {
				flag = true;
				sqlSession.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		if(storageRecordMapper.modify(bill)>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public int count(StorageRecord record) {
		int count = storageRecordMapper.count(record);
/*		try {
			sqlSession = MyBatisUtil.createSqlSession();
			count = sqlSession.getMapper(StorageRecordMapper.class).count(record);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		return count;
	}

	@Override
	public int getSupplierId(String supplierId) {
		return storageRecordMapper.getSupplierId(supplierId);
	}
}
